Which of the following statements are true?
A) When you open a file for reading, if the file does not exist, an error occurs.
B) When you open a file for writing, if the file does not exist, a new file is created.
C) When you open a file for reading, if the file does not exist, the program will open an empty file.
D) When you open a file for writing, if the file exists, the existing file is overwritten with the new file.
E) When you open a file for writing, if the file does not exist, an error occurs.
In [4]:
f = open('b.py', 'r')
True statments:
A) When you open a file for reading, if the file does not exist, an error occurs.
B) When you open a file for writing, if the file does not exist, a new file is created.
D) When you open a file for writing, if the file exists, the existing file is overwritten with the new file.
Examine the following three functions that take as argument a file name and return the extension of that file. For instance, if the file name is 'myfile.tar.gz' the returned value of the function should be 'gz'. If the file name has no extention, i.e. when the file name is just 'myfile', the function should return an empty string.
def get_extension1(filename):
return(filename.split(".")[-1])
def get_extension2(filename):
import os.path
return(os.path.splitext(filename)[1])
def get_extension3(filename):
return filename[filename.rfind('.'):][1:]
Which of the these functions are doing exactly what they are supposed to do according to the description above?
In [7]:
def get_extension1(filename):
return(filename.split(".")[-1])
def get_extension2(filename):
import os.path
return(os.path.splitext(filename)[1])
def get_extension3(filename):
return filename[filename.rfind('.'):][1:]
In [9]:
print "get_extension1"
print "get_extension1('filename'):%s" % get_extension1('filename')
print "get_extension1('filename.py'):%s" % get_extension1('filename.py')
print
print "get_extension2"
print "get_extension2('filename'):%s" % get_extension2('filename')
print "get_extension2('filename.py'):%s" % get_extension2('filename.py')
print
print "get_extension3"
print "get_extension3('filename'):%s" % get_extension3('filename')
print "get_extension3('filename.py'):%s" % get_extension3('filename.py')
# -*- coding: utf-8 -*-
# !/usr/bin/python
import sys
tocheck=sys.argv[1]
print "sys.argv[0]:", sys.argv[0]
print "sys.argv[1]:", sys.argv[1]
print "sys.argv[2]:", sys.argv[2]
# -*- coding: utf-8 -*-
# !/usr/bin/python
print 'b'
# -*- coding: utf-8 -*-
# !/usr/bin/python
print 'c'
After I run python a.py b.py c.py, it shows below:
sys.argv[0]: a.py
sys.argv[1]: b.py
sys.argv[2]: c.py
A student launches the Python interpreter from his home directory. His home directory contains another directory called 'mydir'. What will happen when he write the following code at the Python prompt:
>>> import os
>>> filenames = os.listdir('mydir')
>>> f= open(filenames[0])
Because of current directory is not 'mydir', which just is a child directory, An error will be produced stating that the file to be opened does not exist.